package com.gzhryc.shared_device.oem.micro.api.controllers.xcx.v1.member;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.time.DateUtils;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.api.SecurityResponse;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.cdb.code.dao.db.CDBOrder;
import com.gzhryc.shared_device.cdb.code.services.CDBOrderService;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZOrder;
import com.gzhryc.shared_device.cdcz.code.services.CDCZOrderService;
import com.gzhryc.shared_device.oem.code.device.EDeviceFunCode;
import com.gzhryc.shared_device.oem.code.member.LoginMember;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberBusinessOrder;
import com.gzhryc.shared_device.oem.code.member.services.MemberBusinessOrderService;
import com.gzhryc.shared_device.oem.code.member.services.dto.MemberBusinessOrderSearch;
import com.gzhryc.shared_device.oem.micro.api.MicroConstants;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.ClientAPICodeConstants;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.DefaultResponse;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.cdb.CDBMemberOrderInfoResponse;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.cdcz.CDCZMemberOrderInfoResponse;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.member.MemberOrderInfoRequest;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.member.MemberOrderInfoResponse;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.member.MemberOrderListRequest;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.member.MemberOrderListResponse;

public class MemberOrderController {

    static Logger log = Logger.getLogger(MemberOrderController.class);

    @Route(value = { "/xcx/v1/member/order/list.data" }, method = "POST")
    public MemberOrderListResponse orderList(HttpServletRequest request) {
        MemberOrderListResponse apiResp = new MemberOrderListResponse(ClientAPICodeConstants.NULL_DATA, "暂无订单");
        MemberOrderListRequest apiReq = WebUtils.getJsonRequest(request, MemberOrderListRequest.class);
        if (apiReq != null && apiReq.getBody().getPageNo() != null) {
            LoginMember member = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);

            Date currentDate = new Date();
            if(StringTools.isNotBlank(apiReq.getBody().getYear())){
                try {
                    currentDate = DateUtils.parseDate(apiReq.getBody().getYear(),"yyyy");
                } catch (ParseException e) {
                    log.error(e.getMessage(),e);
                }
            }

            MemberBusinessOrderSearch search = new MemberBusinessOrderSearch();
            search.setMemberId(member.getId());
            search.setState(apiReq.getBody().getState());
            List<MemberBusinessOrder> orderList = MemberBusinessOrderService.self(currentDate).findBySearch(search,apiReq.getBody().getPageNo(),MicroConstants.PAGE_LIMIT);
            if(orderList != null && orderList.size() > 0){
                apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
                apiResp.getBody().setOrderItemList(new ArrayList<>());

                for(MemberBusinessOrder order : orderList) {
                    MemberOrderListResponse.OrderItem item = new MemberOrderListResponse.OrderItem();
                    item.setTradeNo(order.getTradeNo());
                    item.setTitle(order.getTitle());
                    item.setCostMode(order.getCostMode());
                    item.setCostModeName(order.getCostModeName());
                    item.setBusinessType(order.getBusinessType());
                    item.setBusinessTypeName(order.getBusinessTypeName());
                    item.setDeviceFuncCode(order.getDeviceFunCode());
                    item.setDeviceFuncCodeName(order.getDeviceFunCodeName());
                    item.setDeviceTypeName(order.getDeviceTypeName());
                    item.setDeviceSn(order.getDeviceSn());
                    item.setSlotNum(order.getSlotNum());
                    item.setPlaceName(order.getPlaceName());
                    item.setProvince(order.getProvince());
                    item.setCity(order.getCity());
                    item.setMoneyStr(order.getMoneyStr());
                    item.setState(order.getState());
                    item.setStateName(order.getStateName());
                    item.setCreateDate(order.getCreateDate());
                    apiResp.getBody().getOrderItemList().add(item);
                }
            }
        }
        return apiResp;
    }

    @Route(value = { "/xcx/v1/member/order/info.data" }, method = "POST")
    public SecurityResponse<?> orderInfo(HttpServletRequest request) {
        MemberOrderInfoRequest apiReq = WebUtils.getJsonRequest(request, MemberOrderInfoRequest.class);
        if (apiReq != null && apiReq.getBody().getTradeNo() != null) {
            Date currentDate = new Date();
            if(StringTools.isNotBlank(apiReq.getBody().getCreateDate())){
                try {
                    currentDate = DateUtils.parseDate(apiReq.getBody().getCreateDate(),"yyy-MM-dd HH:mm:ss","yyy-MM-dd");
                } catch (ParseException e) {
                    log.error(e.getMessage(),e);
                }
            }

            LoginMember member = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
            MemberBusinessOrder businessOrder = MemberBusinessOrderService.self(currentDate).getByMemberAndTradeNo(member.getId()
                    ,apiReq.getBody().getTradeNo());
            if(businessOrder != null){
                if(EDeviceFunCode.CDCZ.index().equals(businessOrder.getDeviceFunCode())){
                    CDCZMemberOrderInfoResponse apiResp = new CDCZMemberOrderInfoResponse();
                    apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
                    apiResp.getBody().setTradeNo(businessOrder.getTradeNo());
                    apiResp.getBody().setDeviceFuncCode(businessOrder.getDeviceFunCode());
                    apiResp.getBody().setDeviceFuncCodeName(businessOrder.getDeviceFunCodeName());
                    apiResp.getBody().setCostMode(businessOrder.getCostMode());
                    apiResp.getBody().setCostModeName(businessOrder.getCostModeName());

                    apiResp.getBody().setDeviceSn(businessOrder.getDeviceSn());
                    apiResp.getBody().setSlotNum(businessOrder.getSlotNum());

                    apiResp.getBody().setPlaceName(businessOrder.getPlaceName());

                    apiResp.getBody().setPayType(businessOrder.getPayType());
                    apiResp.getBody().setPayTypeName(businessOrder.getPayTypeName());
                    apiResp.getBody().setMoneyStr(businessOrder.getMoneyStr());
                    apiResp.getBody().setDiscountMoneyStr(businessOrder.getDiscountMoneyStr());
                    apiResp.getBody().setPayMoneyStr(businessOrder.getPayMoneyStr());
                    apiResp.getBody().setCouponTitle(businessOrder.getCouponTitle());

                    apiResp.getBody().setState(businessOrder.getState());
                    apiResp.getBody().setStateName(businessOrder.getStateName());
                    apiResp.getBody().setCreateDate(businessOrder.getCreateDate());

                    CDCZOrder cdczOrder = CDCZOrderService.self(businessOrder.getCreateDate()).getByTradeNo(businessOrder.getTradeNo());
                    if(cdczOrder != null){
                        apiResp.getBody().setLatitude(cdczOrder.getLatitude());
                        apiResp.getBody().setLongitude(cdczOrder.getLongitude());
                        apiResp.getBody().setPlaceAddress(cdczOrder.getFullAddress());

                        apiResp.getBody().setStartDate(cdczOrder.getStartDate());
                        apiResp.getBody().setEndDate(cdczOrder.getEndDate());
                        apiResp.getBody().setMaxUsePower(cdczOrder.getMaxUsePower());
                        apiResp.getBody().setMaxPayMoneyStr(cdczOrder.getMaxPayMoneyStr());
                        apiResp.getBody().setUseTimesStr(cdczOrder.getUseTimesStr());
                        apiResp.getBody().setStopModeName(cdczOrder.getStopModeName());
                    }else{
                        log.error("未找到充电插座设备订单，订单号：{{0}}",businessOrder.getTradeNo());
                    }

                    return apiResp;
                }else if(EDeviceFunCode.CDB.index().equals(businessOrder.getDeviceFunCode())){
                    CDBMemberOrderInfoResponse apiResp = new CDBMemberOrderInfoResponse();
                    apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
                    apiResp.getBody().setTradeNo(businessOrder.getTradeNo());
                    apiResp.getBody().setDeviceFuncCode(businessOrder.getDeviceFunCode());
                    apiResp.getBody().setDeviceFuncCodeName(businessOrder.getDeviceFunCodeName());
                    apiResp.getBody().setCostMode(businessOrder.getCostMode());
                    apiResp.getBody().setCostModeName(businessOrder.getCostModeName());

                    apiResp.getBody().setLeaseDeviceSn(businessOrder.getDeviceSn());
                    apiResp.getBody().setLeaseSlotNum(businessOrder.getSlotNum());

                    apiResp.getBody().setLeasePlaceName(businessOrder.getPlaceName());

                    apiResp.getBody().setPayType(businessOrder.getPayType());
                    apiResp.getBody().setPayTypeName(businessOrder.getPayTypeName());
                    apiResp.getBody().setMoneyStr(businessOrder.getMoneyStr());
                    apiResp.getBody().setDiscountMoneyStr(businessOrder.getDiscountMoneyStr());
                    apiResp.getBody().setPayMoneyStr(businessOrder.getPayMoneyStr());
                    apiResp.getBody().setCouponTitle(businessOrder.getCouponTitle());

                    apiResp.getBody().setState(businessOrder.getState());
                    apiResp.getBody().setStateName(businessOrder.getStateName());
                    apiResp.getBody().setCreateDate(businessOrder.getCreateDate());

                    CDBOrder cdbOrder = CDBOrderService.self(businessOrder.getCreateDate()).getByTradeNo(businessOrder.getTradeNo());
                    if(cdbOrder != null){
                        apiResp.getBody().setPortableBatterySn(cdbOrder.getPortableBatterySn());
                        apiResp.getBody().setRevertDeviceSn(cdbOrder.getRevertChargingCabinetSn());
                        apiResp.getBody().setRevertSlotNum(cdbOrder.getRevertSlotNum());

                        apiResp.getBody().setLeaseLatitude(cdbOrder.getLeaseLatitude());
                        apiResp.getBody().setLeaseLongitude(cdbOrder.getLeaseLongitude());
                        apiResp.getBody().setLeasePlaceAddress(cdbOrder.getLeaseFullAddress());

                        apiResp.getBody().setRevertPlaceName(cdbOrder.getRevertPlaceName());
                        apiResp.getBody().setRevertPlaceAddress(cdbOrder.getRevertFullAddress());
                        apiResp.getBody().setRevertLatitude(cdbOrder.getRevertLatitude());
                        apiResp.getBody().setRevertLongitude(cdbOrder.getRevertLongitude());

                        apiResp.getBody().setDepositMoneyStr(cdbOrder.getDepositMoneyStr());
                        apiResp.getBody().setPayIntervalMoneyStr(cdbOrder.getPayIntervalMoneyStr());
                        apiResp.getBody().setDayMaxMoneyStr(cdbOrder.getDayMaxMoneyStr());

                        apiResp.getBody().setUseDurationStr(cdbOrder.getUseDurationStr());
                        apiResp.getBody().setStartDate(cdbOrder.getStartDate());
                        apiResp.getBody().setEndDate(cdbOrder.getEndDate());
                    }else{
                        log.error("未找到充电宝设备订单，订单号：{{0}}",businessOrder.getTradeNo());
                    }
                    return apiResp;
                }else{
                    MemberOrderInfoResponse apiResp = new MemberOrderInfoResponse();
                    apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
                    apiResp.getBody().setTradeNo(businessOrder.getTradeNo());
                    apiResp.getBody().setDeviceFuncCode(businessOrder.getDeviceFunCode());
                    apiResp.getBody().setDeviceFuncCodeName(businessOrder.getDeviceFunCodeName());
                    apiResp.getBody().setCostMode(businessOrder.getCostMode());
                    apiResp.getBody().setCostModeName(businessOrder.getCostModeName());

                    apiResp.getBody().setDeviceSn(businessOrder.getDeviceSn());
                    apiResp.getBody().setSlotNum(businessOrder.getSlotNum());

                    apiResp.getBody().setPlaceName(businessOrder.getPlaceName());

                    apiResp.getBody().setPayType(businessOrder.getPayType());
                    apiResp.getBody().setPayTypeName(businessOrder.getPayTypeName());
                    apiResp.getBody().setMoneyStr(businessOrder.getMoneyStr());
                    apiResp.getBody().setDiscountMoneyStr(businessOrder.getDiscountMoneyStr());
                    apiResp.getBody().setPayMoneyStr(businessOrder.getPayMoneyStr());
                    apiResp.getBody().setCouponTitle(businessOrder.getCouponTitle());

                    apiResp.getBody().setState(businessOrder.getState());
                    apiResp.getBody().setStateName(businessOrder.getStateName());
                    apiResp.getBody().setCreateDate(businessOrder.getCreateDate());
                    return apiResp;
                }
            }
        }
        return new DefaultResponse(ClientAPICodeConstants.NULL_DATA, "未找到订单信息");
    }
}
